self.onmessage = async function (e) {
  const { file, quality = 0.7, index } = e.data;

  // 使用 createImageBitmap 替代 new Image()
  const bitmap = await createImageBitmap(file);

  const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
  const ctx = canvas.getContext('2d');

  let width = bitmap.width;
  let height = bitmap.height;
  const MAX_WIDTH = 1024;

  if (width > height && width > MAX_WIDTH) {
    height = (height * MAX_WIDTH) / width;
    width = MAX_WIDTH;
  } else if (height > MAX_WIDTH) {
    width = (width * MAX_WIDTH) / height;
    height = MAX_WIDTH;
  }

  canvas.width = width;
  canvas.height = height;

  // 绘制缩放后的图像到 canvas
  ctx.drawImage(bitmap, 0, 0, width, height);

  // 压缩成 Blob
  const blob = await canvas.convertToBlob({
    type: 'image/jpeg',
    quality
  });

  // 创建压缩后的 File 对象
  const compressedFile = new File([blob], file.name, {
    type: 'image/jpeg',
    lastModified: Date.now()
  });

  // 返回压缩结果给主线程
  self.postMessage({
    index,
    url: URL.createObjectURL(blob),
    file: compressedFile
  });
};
